import { defineStore } from 'pinia'

export const useMenuStore = defineStore('menuStore', {
  state: () => {
    return {
      menus: [] as Array<menu>,
    }
  },
  getters: {
    Menus: (state) => state.menus,
    getChildren: (state) => {
      return (item: menu | undefined) =>
        state.menus.filter((x) => (x.parentName || '') == (item?.name || ''))
    },
  },
  actions: {
    pushMenum(menus: Array<menu>) {
      if (!this.menus) this.menus = []
      for (let i = 0; i < menus.length; i++) {
        this.menus.push(menus[i])
      }
    },
    getMenuPath(item: menu): Array<menu> {
      if (!item.parentName) {
        return [item]
      } else {
        const parentItem = this.menus.find((x) => x.name == item.parentName)
        if (parentItem) {
          const parents = this.getMenuPath(parentItem)
          parents.push(item)
          return parents
        }
        return [item]
      }
    },
  },
})

export interface menu {
  name: string
  url: string
  // 不可见的
  invisible?: boolean
  icon?: string
  parentName?: string
  // children?: Array<menu>
}
